package com.hundsun.s3.platform.backstage.service.Impl;

import com.hundsun.s3.common.dto.ClientPageDTO;
import com.hundsun.s3.common.dto.DeptDTO;
import com.hundsun.s3.common.dto.Message;
import com.hundsun.s3.common.dto.OrderDTO;
import com.hundsun.s3.common.util.BaseDao;
import com.hundsun.s3.platform.service.BackClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author:肖文轩
 * @date:2018/3/23
 **/
@Service
public class BackClientServiceImpl extends BaseDao implements BackClientService {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 增加用户
     * @param username
     * @param password
     * @param sex
     * @param nickname
     * @param mailbox
     * @param birthday
     * @param phonenumber
     * @param dept_tree
     * @return
     */
    @Override
    public String register(String username, String password, int sex, String nickname, String mailbox, String birthday, String phonenumber, String dept_tree) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("username",username);
        map.put("password",password);
        map.put("sex",sex);
        map.put("nickname",nickname);
        map.put("mailbox",mailbox);
        map.put("birthday",Integer.parseInt(birthday.replace("-","")));
        map.put("phonenumber",phonenumber);
        if(dept_tree == null || dept_tree.equals("") || dept_tree.equals("null")) dept_tree = "1";
        map.put("dept",dept_tree);
        String result = this.exc("800001",map);
        if("执行成功".equals(result)) {
            return "1";
        }else {
            return result;
        }
    }

    /**
     * 修改用户信息
     * @param username
     * @param nickname
     * @param phonenumber
     * @param dept_tree
     * @return
     */
    @Override
    public String updateUserInfo(String username, String nickname, String phonenumber, String dept_tree) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("nickname",nickname);
        map.put("phonenumber",phonenumber);
        if(dept_tree == null || dept_tree.trim().equals("") || dept_tree.equals("null")) dept_tree = "1";
        map.put("dept",dept_tree);
        String resultMsg = this.exc("800008",map);
        if("执行成功".equals(resultMsg)) {
            return "1";
        }else {
            return resultMsg;
        }
    }

    /**
     * 判断用户是否存在
     * @param username
     * @return
     */
    @Override
    public int judgeUsername(String username) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        String resultMsg = this.exc("800009",map);
        return Integer.parseInt(resultMsg);
    }

    /**
     * 分页查询用户
     * @param pageNo
     * @param pageSize
     * @param username
     * @param nickname
     * @param mailbox
     * @param phonenumber
     * @param dept_tree
     * @return
     */
    @Override
    public List<ClientPageDTO> findPage(int pageNo, int pageSize, String username, String nickname, String mailbox, String phonenumber, String dept_tree) {
        if(username == null || username.equals("")) username = "%";
        if(nickname == null || nickname.equals("")) nickname = "%";
        if(mailbox == null || mailbox.equals("")) mailbox = "%";
        if(phonenumber == null || phonenumber.equals("")) phonenumber = "%";
        if(dept_tree == null || dept_tree.trim().equals("")) dept_tree = "%";
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",pageNo);
        map.put("pageSize",pageSize);
        map.put("username",username);
        map.put("nickname",nickname);
        map.put("mailbox",mailbox);
        map.put("dept",dept_tree);
        map.put("phonenumber",phonenumber);
        List<ClientPageDTO> list = this.qry("800006",map,ClientPageDTO.class);
        return list;
    }

    /**
     * 获取分页总数
     * @param username
     * @param nickname
     * @param mailbox
     * @param phonenumber
     * @param dept_tree
     * @return
     */
    @Override
    public int getTotal(String username, String nickname, String mailbox, String phonenumber, String dept_tree) {
        if(username == null || username.equals("")) username = "%";
        if(nickname == null || nickname.equals("")) nickname = "%";
        if(mailbox == null || mailbox.equals("")) mailbox = "%";
        if(phonenumber == null || phonenumber.equals("")) phonenumber = "%";
        if(dept_tree == null || dept_tree.equals("")) dept_tree = "%";
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("nickname",nickname);
        map.put("mailbox",mailbox);
        map.put("dept",dept_tree);
        map.put("phonenumber",phonenumber);
        String resultMsg = this.exc("800010",map);
        return Integer.parseInt(resultMsg);
    }

    /**
     * 验证邮箱是否已经注册
     * @param mailbox
     * @return
     */
    @Override
    public int judgeMailbox(String mailbox) {
        Map<String,Object> map = new HashMap<>();
        map.put("mailbox",mailbox);
        String resultMsg = this.exc("800013",map);
        return Integer.parseInt(resultMsg);
    }

    /**
     * 用户注销
     * @param username
     * @return
     */
    @Override
    public String cancle(String username) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        String resultMsg = this.exc("800015",map);
        if("执行成功".equals(resultMsg)) {
            return "1";
        }else {
            return resultMsg;
        }
    }

    /**
     * 注销恢复
     * @param username
     * @return
     */
    @Override
    public String cancleReopen(String username) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        String resultMsg = this.exc("800016",map);
        if("执行成功".equals(resultMsg)) {
            return "1";
        }else {
            return resultMsg;
        }
    }

    /**
     * 判断用户是否已注销
     * @param username
     * @return
     */
    @Override
    public int judgeUsernameCancle(String username) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        String resultMsg = this.exc("800014",map);
        try {
            Integer.parseInt(resultMsg);
        }catch (Exception e) {
            return 0;
        }
        return Integer.parseInt(this.exc("800014",map));
    }

    /**
     * 获取部门集合
     * @return
     */
    @Override
    public List<DeptDTO> getDeptList() {
        return this.qry("800017",new HashMap<String, Object>(),DeptDTO.class);
    }

    /**
     * 根据用户名查纪录
     * @param client_id
     * @param staff_id
     * @param start_time
     * @param end_time
     * @return
     */
    @Override
    public List<Message> getMessageByUsername(String client_id, String staff_id, long start_time, long end_time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            start_time = sdf.parse(String.valueOf(start_time)).getTime();
            end_time = sdf.parse(String.valueOf(end_time)).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String start = String.valueOf(start_time);
        String end = String.valueOf(end_time);

        Query query = new Query(new Criteria().orOperator(Criteria.where("fromId").is(client_id).and("toId").is(staff_id).and("timestamp").gt(start).lt(end),
                Criteria.where("toId").is(client_id).and("fromId").is(staff_id).and("timestamp").gt(start).lt(end)));
        return mongoTemplate.find(query,Message.class,client_id);
    }

    /**
     * 根据用户名获取工单
     * @param pageNo
     * @param pageSize
     * @param username
     * @param start_time
     * @param end_time
     * @return
     */
    @Override
    public List<OrderDTO> getOrderByClientId(int pageNo, int pageSize, String username, long start_time, long end_time) {
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",pageNo);
        map.put("pageSize",pageSize);
        map.put("client_name",username);
        map.put("server_start_time",start_time);
        map.put("server_end_time",end_time);
        List<OrderDTO> list = this.qry("800034",map,OrderDTO.class);
        for (OrderDTO o:list) {
            o.setUsername(username);
        }
        return list;
    }

    /**
     * 获取工单总数
     * @param pageNo
     * @param pageSize
     * @param username
     * @param start_time
     * @param end_time
     * @return
     */
    @Override
    public int getOrderTotal(int pageNo, int pageSize, String username, long start_time, long end_time) {
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",pageNo);
        map.put("pageSize",pageSize);
        map.put("client_name",username);
        map.put("server_start_time",start_time);
        map.put("server_end_time",end_time);
        return Integer.parseInt(this.exc("800035",map));
    }

}
